package com.xuan.yupao.controller;


import com.xuan.yupao.common.ErrorCode;
import com.xuan.yupao.common.Result;
import com.xuan.yupao.exception.BusinessException;
import com.xuan.yupao.model.domain.User;
import com.xuan.yupao.model.domain.request.FriendAddRequest;
import com.xuan.yupao.model.domain.vo.AddRequestVo;
import com.xuan.yupao.model.domain.vo.UserVo;
import com.xuan.yupao.service.FriendService;
import com.xuan.yupao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/friends")
public class FriendController {

    @Resource
    private FriendService friendService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public Result<String> addFriend(@RequestBody FriendAddRequest friendAddRequest, HttpServletRequest request) {
        if (friendAddRequest == null || friendAddRequest.getReceiveId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        log.info("[addFriend] friendAddRequest:{}", friendAddRequest);

        User loginUser = userService.getLoginUser(request);
        Long toId = friendAddRequest.getReceiveId();
        User user = userService.getById(toId);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        }
        String result = friendService.addFriend(loginUser.getId(), toId, friendAddRequest.getRequestContent());
        return Result.success(result);
    }


    @PostMapping("/agree/{userId}")
    public Result<String> agreeFriendApply(@PathVariable(value = "userId") Long userId, HttpServletRequest request) {
        if (userId == null || userId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }

        User loginUser = userService.getLoginUser(request);
        User user = userService.getById(userId);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        }
        String result = friendService.agreeFriendApply(userId, loginUser.getId());
        return Result.success(result);
    }



    @PostMapping("/refuse/{userId}")
    public Result<String> refuseFriendApply(@PathVariable(value = "userId") Long userId, HttpServletRequest request) {
        if (userId == null || userId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }

        User loginUser = userService.getLoginUser(request);
        User user = userService.getById(userId);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        }
        String result = friendService.refuseFriendApply(userId, loginUser.getId());
        return Result.success(result);
    }


    @GetMapping("/untreated/num")
    public Result<Long> getFriendRequestNum(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long num = friendService.getFriendRequestNum(loginUser.getId());
        return Result.success(num);
    }


    @GetMapping("/getUntreatedRecords")
    public Result<List<AddRequestVo>> getUntreatedRecords(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<AddRequestVo> addRequestVoList = friendService.getUntreatedRecords(loginUser.getId());
        return Result.success(addRequestVoList);
    }


    @GetMapping("/my")
    public Result<List<UserVo>> getMyFriendList(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<UserVo> userVoList = friendService.getMyFriendList(loginUser.getId());
        return Result.success(userVoList);
    }
}
